package cn.jobstart.es.listener;

import cn.jobstart.common.BeanUtils;
import cn.jobstart.es.RsOperationFlagEnum;
import cn.jobstart.es.listener.event.CompanyEsEvent;
import cn.jobstart.es.model.CompanyEs;
import cn.jobstart.es.service.ICompanyEsService;
import cn.jobstart.modelExt.CompanyExt;
import cn.jobstart.service.ICompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author sunke
 * @version 1.0
 * @Description 企业Es推送
 * @date 2023-09-22 11:00
 */
@Slf4j
@Component
public class CompanyEsListener implements IEsListener<CompanyEsEvent> {


    @Resource
    private ICompanyService companyService;

    @Resource
    private ICompanyEsService companyEsService;


    @EventListener(CompanyEsEvent.class)
    @Override
    public void action(CompanyEsEvent event) {

        try {
            int tye = event.getType();

            String id = event.getCompanyId();
            CompanyExt company = companyService.getDetail(id);
            CompanyEs companyEs = new CompanyEs();
            BeanUtils.copyPropertiesIgnoreNull(company, companyEs);

            companyEs.setId(company.getId());

            if (tye == RsOperationFlagEnum.ADD.getTypeFlag()) {
                companyEsService.insert(companyEs);
                return;
            }
            if (tye == RsOperationFlagEnum.UPDATE.getTypeFlag()) {
                companyEsService.update(companyEs,companyEs.getId());
                return;
            }

            if (tye == RsOperationFlagEnum.DELETE.getTypeFlag()) {
                companyEsService.delete(id);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
